package com.abc.invoke.client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.abc.invoke.bean.User;
import com.abc.invoke.bean.UserSessionToken;
import com.abc.invoke.client.config.InvokerClientConfig;
import com.abc.invoke.services.interfaces.AccessControlInterface;

public class AbstractClient {

	volatile AccessControlInterface aci;
    private volatile User currentUser;
	public AbstractClient() {

	}

	public User login(String name, String password){
		User user=this.getACI().login(name, password);
		if(user!=null){
			currentUser=user;
		}
		return user;
	}
	
	public User loadUserProfile(String name){
		System.out.println("current user: "+currentUser.getName());
		User user=this.getACI().loadUserProfile(this.getSessionToken(), name);
		
		return user;
	}
	
	public AccessControlInterface getACI() {
		if (aci != null)
			return aci;
		ApplicationContext ac = new AnnotationConfigApplicationContext(InvokerClientConfig.class);
		AccessControlInterface service = (AccessControlInterface) ac.getBean("accessService");
		this.aci = service;
		return service;
	}
	
	public UserSessionToken getSessionToken(){
		return this.currentUser.getToken();
	}
}
